//
//  UserAccount.swift
//  传智小微博
//
//  Created by lxgoodgo on 16/4/19.
//  Copyright © 2016年 liux.orz. All rights reserved.
//

import UIKit
// 用户模型  用于获取到用户数据后 把用户数据对档  以后直接解挡 就可以用了
class UserAccount: NSObject,NSCoding {
    
    //登录令牌 用于判断用户是否登录过
    var access_token: String?
    
    //expires_in access_token的生命周期，单位是秒数。 
    //计算出 过期日期
    var expires_in : Int = 0
    {
        didSet{
            expires_Date = NSDate().dateByAddingTimeInterval(Double(expires_in))
        }
    }
    // 授权用户的UID
    var uid: String?
    
    //昵称
    var screen_name: String?

    //微博头像
    var avatar_large : String?
    
    // 便于我们开发使用判断我们的token是否过期
    var expires_Date: NSDate?
    
    init(dict: [String : AnyObject])
    {
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //防止 报错
    override func setValue(value: AnyObject?, forUndefinedKey key: String) {
        
    }
    
    override var description: String
    {
        let keys = ["access_token", "expires_Date", "uid", "avatar_large", "screen_name"]
        return dictionaryWithValuesForKeys(keys).description
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
        aCoder.encodeObject(access_token,forKey:"access_token")
        aCoder.encodeObject(uid,forKey:"uid")
        aCoder.encodeObject(screen_name,forKey:"screen_name")
        aCoder.encodeObject(avatar_large,forKey:"avatar_large")
        aCoder.encodeObject(expires_Date,forKey:"expires_Date")
    }
    
    //解挡
    required init?(coder aDecoder: NSCoder) {
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        screen_name = aDecoder.decodeObjectForKey("screen_name") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        expires_Date = aDecoder.decodeObjectForKey("expires_Date") as? NSDate
        
    }
    
   
    
    
    
    
}
